package start.spring.basic.response;

import org.springframework.cloud.context.config.annotation.RefreshScope;

/**
 * 
 * ****************************************************************************
 * 统一的系统相应对象
 *
 * @author(作者)：xuyongyun	
 * @date(创建日期)：2020年4月9日
 ******************************************************************************
 */
@RefreshScope
public class ResponseData<T> {
	
	public static final String DEFAULT_SUCCESS_CODE = "200";
    public static final String DEFAULT_ERROR_CODE = "500";
    
	public static final String DEFAULT_SUCCESS_MESSAGE = "请求成功";
    public static final String DEFAULT_ERROR_MESSAGE = "系统异常";
    
    private String code;
    private String msg;
    private T data;
    
    public ResponseData() {}
    
    public ResponseData(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    
    public ResponseData(String code, String msg, T obj) {
        this.code = code;
        this.msg = msg;
        this.data = obj;
    }
    
    public static <T> ResponseData<T> success() {
        return new ResponseData<>(DEFAULT_SUCCESS_CODE,DEFAULT_SUCCESS_MESSAGE);
    }
    
    public static <T> ResponseData<T> successData(T data) {
        return new ResponseData<>(DEFAULT_SUCCESS_CODE,DEFAULT_SUCCESS_MESSAGE,data);
    }
    
    public static <T> ResponseData<T> successMsg(String message) {
        return new ResponseData<>(DEFAULT_SUCCESS_CODE, message);
    }
    
    public static <T> ResponseData<T> success(String message,T data) {
        return new ResponseData<>(DEFAULT_SUCCESS_CODE, message, data);
    }
    
    public static <T> ResponseData<T> error() {
        return new ResponseData<>(DEFAULT_ERROR_CODE,DEFAULT_ERROR_MESSAGE);
    }
    
    public static <T> ResponseData<T> errorData(T data) {
        return new ResponseData<>(DEFAULT_ERROR_CODE,DEFAULT_ERROR_MESSAGE,data);
    }
    
    public static <T> ResponseData<T> errorMsg(String message) {
        return new ResponseData<>(DEFAULT_ERROR_CODE,message);
    }
    
    public static <T> ResponseData<T> error(String message,T data) {
        return new ResponseData<>(DEFAULT_ERROR_CODE,message,data);
    }

    public static <T> ResponseData<T> error(String code, String message) {
        return new ResponseData<>(code, message);
    }
    
    public static <T> ResponseData<T> error(String code, String message,T data) {
        return new ResponseData<>(code, message,data);
    }

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
}
